/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.ellematica.sampledownloadpdf.struts.action;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author user
 */
public class Test {

      private static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        if (length > Integer.MAX_VALUE) {
         }
         byte[] bytes = new byte[(int)length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
    is.close();
        return bytes;
    }

    public static int downloadPdf(String pdfName){
        int pdfLength = -1;
         try {

            
          

            InputStream ISPdfDocument =Test.class.getResourceAsStream("..\\..\\resources\\"+pdfName);


            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int len;

            while((len = ISPdfDocument.read(buffer)) >= 0){
                baos.write(buffer, 0, len);
            }
            ISPdfDocument.close();
            baos.close();
            byte[] pdfDocument = baos.toByteArray();
            pdfLength = pdfDocument.length;
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
         return pdfLength;
    }

    public static void main(String[] args) {
		System.out.println("Start");

        String pdfName = "Liferay_Portal_Setup_Guide.pdf";

        int pdfLength = downloadPdf(pdfName);
        
        System.out.println("lenth " +pdfLength);
        




        System.out.println("End");
	}


}
